package com.oceansoft.mobile.biz.front.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.oceansoft.mobile.biz.front.entity.PrizeWinner;
import com.oceansoft.mobile.common.dao.ICommonDao;


@Service
public class GuaGuaServiceImpl implements IGuaGuaService{
	@Resource
	private ICommonDao commonDao;
	
	@Override
	public List<PrizeWinner> isJoin(String weixinId){
		String sql = "select * from t_winner where weixinid='"+weixinId+"'";
		return commonDao.loadList(sql,PrizeWinner.getMap());
	}
	
	/*
	 * 随机获得奖品
	 */
	public int PrizeRandom(){
		  
		    int b=(int)(java.lang.Math.random()*4);
		    int count1=commonDao.getPrizeCount("警用水壶");
		    int count2=commonDao.getPrizeCount("随身wifi");
		    int count3=commonDao.getPrizeCount("美甲套装");
		    int count4=commonDao.getPrizeCount("耳机收纳包");
		    if (b==0&count1>0){
		    	count1--;
		    	commonDao.setPrizeCount(count1,"警用水壶");
		    	
		    	
		    	return b;
		    	
		    }else if(b==1&count2>0){
		    	count2--;
		    	commonDao.setPrizeCount(count2,"随身wifi");
		    	return b;
		 
		    }else if(b==2&count3>0){
		    	count3--;
		    	commonDao.setPrizeCount(count3,"美甲套装");
		    	
		    	return b;
		    }else if(b==3&count4>0){
		    	count4--;
		    	commonDao.setPrizeCount(count3,"美甲套装");
		    	
		    	return b;
		    }else if(count4>0){
		    	count4--;
		    	commonDao.setPrizeCount(count4,"美甲套装");
		    	return 3;
		    }else{
		    	return 4;
		    }
	}
	
	/**
	 * 获得奖品总数
	 */
	public int getAll(){
		 int count1=commonDao.getPrizeCount("警用水壶");
		    int count2=commonDao.getPrizeCount("随身wifi");
		    int count3=commonDao.getPrizeCount("美甲套装");
		    int count4=commonDao.getPrizeCount("耳机收纳包");
		    int count=count1+count2+count3+count4;
		    return count;
	}
	/**
	 * 插入奖品
	 */
public int insertValue(String mobile,String idCard,int type,String weixinId,int sid){
	
	int c=commonDao.insertWinner(idCard,mobile,type,weixinId,sid);
	return c;
}

/**
 * 判断此微信号是否是第一次登陆
 */
@Override
public boolean ifFirst(String weixinId) {
	int c=commonDao.loadFirst(weixinId);
	
	if(c>0){
		return true;
	}else{
		commonDao.insertFirst(weixinId);
	return false;
	}
}

@Override
public int updateWinner(PrizeWinner winner) {
	//IDCARD,MOBILE,TYPE,WEIXINID,SID
	return commonDao.update("update t_winner set idcard=?,mobile=? where weixinid = ?", winner.getIdCard(),winner.getMobile(),winner.getWeixinId());
}

@Override
public int insertJoinner(String wxId) {
	return commonDao.update("insert into wx_joiner values(null,?,sysdate())",wxId);
}

//
//@Override
//public Result ifFirst(String weixinId) {
//	int c=commonDao.loadFirst(weixinId);
//	int d=commonDao.loadPrize(weixinId);
//	
//	if(c>0&d==0){
//		return new Result(Result.SUCC,0);
//	}else if(c>0&d==1){
//		return new Result(Result.SUCC,1);
//	
//	}else if(c>0&d==2){
//		return new Result(Result.SUCC,2);
//		
//	}else if(c>0&d==3){
//		return new Result(Result.SUCC,3);
//	}else if(c>0&StringUtils.isNotEmpty(d)){
//		commonDao.insertFirst(weixinId);
//		return false;
//	}
//}





}
